package com.atguigu.gulimail.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberResponseVo;
import com.atguigu.gulimail.auth.feign.MemberFeign;
import com.atguigu.gulimail.auth.utils.HttpUtils;
import com.atguigu.gulimail.auth.vo.SocialUser;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.swing.text.html.parser.Entity;
import java.util.HashMap;
import java.util.Map;

@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeign memberFeign;

    @GetMapping("oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, RedirectAttributes redirectAttributes, HttpSession session){

        //根据code换取AccessToken
        /**
         * https://api.weibo.com/oauth2/access_token?
         * client_id=592516962
         * &client_secret=3a8c6bc85e0605351c0e387460b513bf
         * &grant_type=authorization_code
         * &redirect_uri= http://gulimail.com/oauth2.0/weibo/success
         * &code=CODE
         */
        Map<String,String> map =new HashMap<>();
        map.put("client_id","592516962");
        map.put("client_secret","3a8c6bc85e0605351c0e387460b513bf");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.gulimail.com/oauth2.0/weibo/success");
        map.put("code",code);

        try {
            HttpResponse response =
                HttpUtils
                        .doPost("https://api.weibo.com", "/oauth2/access_token", "post", new HashMap<>(), map, new HashMap<>());

            //处理响应结果
            if(response.getStatusLine().getStatusCode()==200){
                //将相应结果封装为对象
                String json = EntityUtils.toString(response.getEntity());
                SocialUser socialUser = JSON.parseObject(json, SocialUser.class);

                //1. 当前用户如果是第一次登录进来，自动注册，与会员表关联
                //2. 登录或者注册相关用户
                R r=memberFeign.login(socialUser);
                if(r.getCode()==0){

                    String data = JSON.toJSONString(r.get("data"));
                    MemberResponseVo vo = JSON.parseObject(data, MemberResponseVo.class);
                    session.setAttribute("loginUser",vo);

                    //成功，重定向回首页
                    return "redirect:http://gulimail.com";

                }else {
                    Map<String,String> errors=new HashMap<>();
                    errors.put("msg",r.getMsg());
                    redirectAttributes.addFlashAttribute("errors",errors);
                    return "redirect:http://auth.gulimail.com/login.html";
                }
            }
        } catch (Exception e) {

            throw new RuntimeException(e);

        }
        //换取失败
        return "redirect:http://auth.gulimail.com/login.html";
    }

}
